package com.linqi.freebie.base

import io.github.aakira.napier.Antilog
import io.github.aakira.napier.LogLevel
import java.nio.charset.Charset

class Utf8ColorAntilog : Antilog() {

    // ANSI 转义颜色
    private val RESET = "\u001B[0m"
    private val RED = "\u001B[31m"
    private val GREEN = "\u001B[32m"
    private val YELLOW = "\u001B[33m"
    private val BLUE = "\u001B[34m"
    private val CYAN = "\u001B[36m"
    private val PURPLE = "\u001B[35m"

    public override fun performLog(priority: LogLevel, tag: String?, throwable: Throwable?, message: String?) {
        val color = when (priority) {
            LogLevel.VERBOSE -> CYAN
            LogLevel.DEBUG -> BLUE
            LogLevel.INFO -> GREEN
            LogLevel.WARNING -> YELLOW
            LogLevel.ERROR -> RED
            LogLevel.ASSERT -> PURPLE
        }

        val out = System.out
        val logText = "$color[${priority.name}] ${tag ?: "Napier日志------------"}: ${message ?: ""}$RESET\n"
        out.write(logText.toByteArray(Charset.forName("UTF-8")))
        throwable?.printStackTrace(out)
    }
}